package vn.personal.anhht.bloback12.activity;

import vn.personal.anhht.bloback12.util.Constants;
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends Activity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		showUnlockDialog();
	}
	int count = 0;
	boolean isLogin = false;
	public void showUnlockDialog(){
		final String pass = getApplicationContext().getSharedPreferences(Constants.SETTINGS_SHARED_PREFERENCES_NAME, MODE_PRIVATE).getString(Constants.KEY_PREF_SETTINGS_PASSWORD, "");
		String passhint = getApplicationContext().getSharedPreferences(Constants.SETTINGS_SHARED_PREFERENCES_NAME, MODE_PRIVATE).getString(Constants.KEY_PREF_SETTINGS_PASSWORD_HINT, getResources().getString(R.string.hint_pass_default));
		if(pass.equals("")){
			finish();
		}else{
			final Dialog d = new Dialog(LoginActivity.this);
			d.setContentView(R.layout.dialog_login);
			d.setTitle(getResources().getString(R.string.login_title));
			TextView tvHint = (TextView) d.findViewById(R.id.tv_login_hint);
			tvHint.setText(getResources().getString(R.string.login_pass_hint) + passhint);
			final EditText etPass = (EditText) d.findViewById(R.id.et_login_pass);
			Button btnLogin = (Button) d.findViewById(R.id.login_btn_ok);
			btnLogin.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					count++;
					if(pass.equals(etPass.getText().toString())){
							d.dismiss();
							goBack(Constants.RESPONSE_LOGIN_OK);
					}else{
							if(count < 3){
								etPass.setText("");
								Toast.makeText(getApplicationContext(), getResources().getString(R.string.login_wrong_pass) + " " + (3-count), Toast.LENGTH_LONG).show();
							}else{
								d.dismiss();
								goBack(Constants.RESPONSE_LOGIN_CANCEL);	
							}
					}
				}
			});
			d.setOnKeyListener(new OnKeyListener() {
				
				@Override
				public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
					// TODO Auto-generated method stub
					if(keyCode == KeyEvent.KEYCODE_BACK){
						d.dismiss();
						goBack(Constants.RESPONSE_LOGIN_CANCEL);
					}else if(keyCode == KeyEvent.KEYCODE_ENTER){
						count++;
						if(pass.equals(etPass.getText().toString())){
								d.dismiss();
								goBack(Constants.RESPONSE_LOGIN_OK);
							
						}else{
								if(count < 3){
									etPass.setText("");
									Toast.makeText(getApplicationContext(), getResources().getString(R.string.login_wrong_pass) + " " + (3-count), Toast.LENGTH_LONG).show();
								}else{
									d.dismiss();
									goBack(Constants.RESPONSE_LOGIN_CANCEL);	
								}
						}
					}
					
					return false;
				}
			});
			d.show();
		}
	}

	public void goBack(int resultCode){
		Intent intentReply = getIntent();
		setResult(resultCode, intentReply);
		finish();
	}
	
	
}
